var express=require('express')
var Router=express.Router()
var db=require('../../tools/database/index.js')

//存储头像
var savImg=require('./SaveImg.js')
//解析post 数据
var PostData=require('../../tools/PostData/index.js')
Router.post('/client/business',async (req,res)=>{
	var data=await PostData(req);
	var query={name:data.name};
	var client=await db.clients.query(query);
	if(client){
		res.send({
			msg:'用户已存请更换用户名'
		})
		return;
	}
	var HeadImg=await savImg(data.HeadImg,`${data.name}-${data._id}`)
	var result=await db.clients.append({
		name:data.name,
		password:data.password,
		HeadImg:HeadImg.url
	})
	if(result){
		res.send({
			msg:'注册成功',
			state:200
		})
	}else{
		res.send({
			msg:'注册失败',
			state:404
		})
	}
})

 module.exports=Router